package com.demo.java8newfeature.stream;


import com.demo.entity.Person;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangning
 * @date 2023/11/20 20:41
 * forEach();
 * filter();
 * map();
 * distinct().collect(Collectors.toList());//去重
 *
 */
public class StreamTest {

    /**
     * 遍历属性
     */
    @Test
    public void test01() {
        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(4);
        list.add(1);
        list.add(3);
        list.add(3);
        list.add(5);
        for (Integer i : list) {
            System.out.println("i = " + i);
        }
        list.stream().forEach(s -> System.out.println(s));
        list.forEach(System.out::println);
        list = list.stream().distinct().collect(Collectors.toList());//去重
        System.out.println("list.stream().count() = " + list.stream().count());//数量
        System.out.println("list.stream().count() = " + (long) list.size());//数量
        System.out.println("list.stream().limit(3) = " + Arrays.toString(list.stream().limit(3).toArray()));//截取前三个
        System.out.println("list.stream().skip(2) = " + Arrays.toString(list.stream().skip(2).toArray()));//跳过前两个

        System.out.println("list.stream().sorted() = " + Arrays.toString(list.stream().sorted().toArray()));//排序
        list.stream().sorted(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {

                return o2-o1;
            }
        });

        System.out.println("list = " + Arrays.toString(list.toArray()));


        System.out.println("list = " + list);
    }


    /**
     * 遍历对象
     */
    @Test
    public void test02() {
        List<Person> list = new ArrayList<>();
        list.add(new Person("张三", 18));
        list.add(new Person("张三", 18));
        list.add(new Person("李四", 17));
        list.add(new Person("王五", 20));
        list.forEach(s -> System.out.println(s.getName()));


    }




}
